﻿

#include "SWidget/SConfigureWindow.h"
#include "Widgets/Layout/SScrollBox.h"
#include "Cache/InputConfigureCacheEditor.h"

#define LOCTEXT_NAMESPACE "WidgetGroupConfigureWindow"

void SConfigureWindow::Construct(const FArguments& InArgs)
{
	if (!InputConfigureLibrary::HasAnyInputConfigFile())
	{
		InputConfigureLibraryEditor::InputConfigFile_Create();
	}

	TSharedPtr<SScrollBox> ListScrollBox;
	TSharedPtr<SScrollBox> ViewScrollBox;

	ChildSlot
		[
			SNew(SVerticalBox)
			+ SVerticalBox::Slot()
		.AutoHeight()
		.Padding(0.0f, 0.0f, 0.0f, 10.0f)
		[
			SNew(SHorizontalBox)
			+ SHorizontalBox::Slot()
		.AutoWidth()
		[
			SNew(SButton)
			.Text(FText::FromString(TEXT("Add New Input Group")))
		.ToolTipText(FText::FromString(TEXT("Create a new widget input group --- 创建一个新的控件输入组")))
		.OnClicked(this, &SConfigureWindow::AddNewGroup)
		]
	+ SHorizontalBox::Slot()
		.AutoWidth()
		.Padding(30.0f, 0.0f, 10.0f, 0.0f)
		[
			SNew(SCheckBox)
			.ToolTipText(FText::FromString(TEXT("The current group is automatically saved after each change --- 每次改动之后都将自动保存当前组")))
		.OnCheckStateChanged(this, &SConfigureWindow::ChangeAutoSave)
		.IsChecked(InputConfigureLibraryEditor::bAutoSave ? ECheckBoxState::Checked : ECheckBoxState::Unchecked)
		[
			SNew(STextBlock)
			.Text(FText::FromString(TEXT("Auto Save Group")))
		]


		]
	+ SHorizontalBox::Slot()
		.AutoWidth()
		.HAlign(HAlign_Right)
		[
			SNew(SButton)
			.Text(FText::FromString(TEXT("Save Current Group")))
		.ToolTipText(FText::FromString(TEXT("Save the current input group --- 保存当前输入组")))
		.OnClicked(this, &SConfigureWindow::SaveThisGroup)
		]
	+ SHorizontalBox::Slot()
		.AutoWidth()
		.Padding(30.0f, 0.0f, 10.0f, 0.0f)
		[
			SAssignNew(DefaultLoadCheckBox, SCheckBox)
			.ToolTipText(FText::FromString(TEXT("The group the game loads by default --- 游戏默认加载的组")))
		.OnCheckStateChanged(this, &SConfigureWindow::SetDefaultLoad)
		[
			SNew(STextBlock)
			.Text(FText::FromString(TEXT("DefalutLoadGroup")))
		]
		]
	+ SHorizontalBox::Slot()
		.AutoWidth()
		.Padding(30.0f, 0.0f, 10.0f, 0.0f)
		[
			SNew(SButton)
			.Text(FText::FromString(TEXT("Create ExampleInput Gourp")))
		.ToolTipText(FText::FromString(TEXT("The key set needed to create the sample content --- 创建示例内容所需要用到的按键组")))
		.OnClicked(this, &SConfigureWindow::CreateExampleInputGourp)
		]
	+ SHorizontalBox::Slot()
		.AutoWidth()
		.Padding(30.0f, 0.0f, 10.0f, 0.0f)
		[
			SNew(STextBlock)
			.Text(FText::FromString(TEXT("插件Q群：1011135423")))
		]
		]
	+ SVerticalBox::Slot()
		.FillHeight(1.0f)
		[
			SNew(SHorizontalBox)
			+ SHorizontalBox::Slot()
		.AutoWidth()
		.Padding(0.0f, 0.0f, 10.0f, 0.0f)
		[
			SNew(SBox)
			.MinDesiredWidth(FOptionalSize(100.0f))
		.MaxDesiredWidth(FOptionalSize(200.0f))
		[
			SAssignNew(ListScrollBox, SScrollBox)
			+ SScrollBox::Slot()
		[
			SAssignNew(InputGroupList, SInputGroupList)
			.UpdateDetailsView(this, &SConfigureWindow::UpdateDetailsView)
		]
		]
		]
	+ SHorizontalBox::Slot()
		.FillWidth(1.0f)
		[
			SNew(SVerticalBox)
			+ SVerticalBox::Slot()
		.AutoHeight()
		.Padding(0.0f, 0.0f, 0.0f, 5.0f)
		[
			SNew(SHorizontalBox)
			+ SHorizontalBox::Slot()
		.AutoWidth()
		.Padding(0.0f, 0.0f, 10.0f, 0.0f)
		[
			SNew(STextBlock)
			.Text(FText::FromString(TEXT("Current Group :")))
		]
	+ SHorizontalBox::Slot()
		.FillWidth(1.0f)
		[
			SAssignNew(GroupKeyEditableText, SEditableText)
			.OnTextCommitted(this, &SConfigureWindow::GroupKeyEditableTextCommitted)
		]
		]
	+ SVerticalBox::Slot()
		.AutoHeight()
		.Padding(0.0f, 0.0f, 0.0f, 5.0f)
		[
			SNew(SHorizontalBox)
			+ SHorizontalBox::Slot()
		.FillWidth(0.5f)
		.Padding(0.0f, 0.0f, 5.0f, 0.0f)
		[
			SNew(SButton)
			.Text(FText::FromString(TEXT("Delete Current Group")))
		.ToolTipText(FText::FromString(TEXT("Delete the current input group --- 删除当前输入组")))
		.OnClicked(this, &SConfigureWindow::DeleteThisGroup)
		]
	+ SHorizontalBox::Slot()
		.FillWidth(0.5f)
		.Padding(5.0f, 0.0f, 0.0f, 0.0f)
		[
			SNew(SButton)
			.Text(FText::FromString(TEXT("Copy Current Group")))
		.ToolTipText(FText::FromString(TEXT("Copy the current input group --- 复制当前输入组")))

		.OnClicked(this, &SConfigureWindow::CopyThisGroup)
		]
		]
	+ SVerticalBox::Slot()
		.FillHeight(1.0f)
		[
			SAssignNew(ViewScrollBox, SScrollBox)
			+ SScrollBox::Slot()
		[
			SAssignNew(ConfigureDetailsView, SConfigureDetailsView)
			.NameChanged(this, &SConfigureWindow::RenameThisGroup)
		]
		]
		]
		]
		];

	ListScrollBox->SetScrollBarRightClickDragAllowed(true);
	ViewScrollBox->SetScrollBarRightClickDragAllowed(true);

	InputGroupList->UpdateList();
}



void SConfigureWindow::UpdateDetailsView(FString GroupKey)
{
	ConfigureDetailsView->UpdateDetailView(GroupKey);
	GroupKeyEditableText->SetText(FText::FromString(GroupKey));
	bool isThisGroup = InputConfigureLibrary::GetDefaultConfigGroupName() == GroupKey;
	DefaultLoadCheckBox->SetEnabled(isThisGroup ? false : true);
	DefaultLoadCheckBox->SetIsChecked(isThisGroup ? ECheckBoxState::Checked : ECheckBoxState::Unchecked);
}

FReply SConfigureWindow::AddNewGroup()
{
	FString NewGroupKey = InputConfigureLibraryEditor::InputConfigFile_Create();
	UpdateDetailsView(NewGroupKey);
	InputGroupList->UpdateList(true);
	return FReply::Handled();
}

FReply SConfigureWindow::DeleteThisGroup()
{
	InputConfigureLibraryEditor::InputConfigFile_Delete(ConfigureDetailsView->GetGroupKey());
	InputGroupList->UpdateList();
	return FReply::Handled();
}

FReply SConfigureWindow::CopyThisGroup()
{
	FString NewName = InputConfigureLibraryEditor::InputConfigFile_Copy(ConfigureDetailsView->GetGroupKey());
	UpdateDetailsView(NewName);
	InputGroupList->UpdateList(true);
	return FReply::Handled();
}

FReply SConfigureWindow::SaveThisGroup()
{
	InputConfigureLibraryEditor::InputConfigFile_Save(ConfigureDetailsView->GetGroup());
	return FReply::Handled();
}

FReply SConfigureWindow::CreateExampleInputGourp()
{
	InputConfigureLibraryEditor::CreateExampleInputGourp();
	InputGroupList->UpdateList(false);
	return FReply::Handled();
}

void SConfigureWindow::RenameThisGroup(FString NewGroupKey)
{
	if (NewGroupKey.IsEmpty())
	{
		ConfigureDetailsView->ResetGroupKey(GroupKeyEditableText->GetText().ToString());
		return;
	}

	if (NewGroupKey != GroupKeyEditableText->GetText().ToString())
	{
		if (InputConfigureLibraryEditor::InputConfigFile_Rename(GroupKeyEditableText->GetText().ToString(), NewGroupKey))
		{
			GroupKeyEditableText->SetText(FText::FromString(NewGroupKey));
			InputGroupList->UpdateList(true);
		}
		else
		{
			ConfigureDetailsView->ResetGroupKey(GroupKeyEditableText->GetText().ToString());
		}
	}
}

void SConfigureWindow::ChangeAutoSave(ECheckBoxState NewAutoSave)
{
	InputConfigureLibraryEditor::bAutoSave = NewAutoSave == ECheckBoxState::Checked ? true : false;
}

void SConfigureWindow::SetDefaultLoad(ECheckBoxState NewDefaultLoad)
{
	if (NewDefaultLoad == ECheckBoxState::Checked)
	{
		InputConfigureLibrary::SetDefaultConfigGroupName(ConfigureDetailsView->GetGroupKey());
		DefaultLoadCheckBox->SetEnabled(false);
	}
}

void SConfigureWindow::GroupKeyEditableTextCommitted(const FText& InText, const ETextCommit::Type InTextAction)
{
	if (InText.IsEmpty())
	{
		GroupKeyEditableText->SetText(FText::FromString(ConfigureDetailsView->GetGroupKey()));
		return;
	}

	if (InText.ToString() != ConfigureDetailsView->GetGroupKey())
	{
		if (InputConfigureLibraryEditor::InputConfigFile_Rename(ConfigureDetailsView->GetGroupKey(), InText.ToString()))
		{
			ConfigureDetailsView->ResetGroupKey(InText.ToString());
			InputGroupList->UpdateList(true);
		}
		else
		{
			GroupKeyEditableText->SetText(FText::FromString(ConfigureDetailsView->GetGroupKey()));
		}
	}
}

#undef LOCTEXT_NAMESPACE

/*FText WidgetText = FText::Format(
		LOCTEXT("WindowWidgetText", "Add code to {0} in {1} to override this window's contents"),
		FText::FromString(TEXT("FInputConfigureButtonModule::OnSpawnPluginTab")),
		FText::FromString(TEXT("InputConfigureButton.cpp"))
	);
	SNew(SBox)
				.HAlign(HAlign_Center)
				.VAlign(VAlign_Center)
				[
					SNew(STextBlock)
					.Text(WidgetText)
				]*/